package sz.szsmk.citizencard.mvp.presenter;

import android.widget.Toast;

import com.google.gson.Gson;

import javax.inject.Inject;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.log.L;
import sz.szsmk.citizencard.model.network.GsdxNetApi;
import sz.szsmk.citizencard.model.network.bean.request.GetCodeRequest;
import sz.szsmk.citizencard.model.network.bean.request.SpecialReq;
import sz.szsmk.citizencard.model.network.bean.response.BaseBean;
import sz.szsmk.citizencard.mvp.contract.ISprcialContact;
import sz.szsmk.citizencard.retrofitrxjava.ErrorAction;
import sz.szsmk.citizencard.retrofitrxjava.NetworkDateSources;
import sz.szsmk.citizencard.retrofitrxjava.SubscribeResult;
import sz.szsmk.citizencard.util.AndroidUtils;
import sz.szsmk.citizencard.util.Constant;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/8/10 18:22
 * description:
 */
public class SpecialPresenter extends IBasePresenter implements
        ISprcialContact.ISprcialPresenter<ISprcialContact.ISprcialView> {

    private NetworkDateSources mNetworkDateSource;
    GsdxNetApi gsdxNetApi;

    @Inject
    public SpecialPresenter(GsdxNetApi gsdxNetApi) {
        this.gsdxNetApi = gsdxNetApi;

        this.mNetworkDateSource = NetworkDateSources.getInstance();
    }

    @Override
    public void modifyPsd(String phone, String cardNo, String oldPsd, String psdNew, String code) {
        SpecialReq specialReq = new SpecialReq();
        specialReq.setCard_no(cardNo);
        specialReq.setMobile(phone);
        specialReq.setNew_password(psdNew);
        specialReq.setOld_password(oldPsd);
        specialReq.setType_id(5);
        specialReq.setSms_code(code);
        String json = new Gson().toJson(specialReq);
        L.e(Constant.TAG, "specialReq----" + json);
        mNetworkDateSource.changeAcctPassword(specialReq, new SubscribeResult<BaseBean>() {
            @Override
            protected void onOk(BaseBean response) {
                ((ISprcialContact.ISprcialView) mView).modifyPsdSucc(response.getMessage());
            }

            @Override
            protected void onNext(BaseBean response) {
                super.onNext(response);
                ((ISprcialContact.ISprcialView) mView).modifyPsdFail(response.getMessage());
            }

            @Override
            protected void onFailure(BaseBean response) {
                super.onFailure(response);
                ((ISprcialContact.ISprcialView) mView).modifyPsdFail(response.getMessage());
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
                ((ISprcialContact.ISprcialView) mView).modifyPsdFail(throwable.getMessage());

            }
        });

    }

    @Override
    public void sprcialGetCode(String userPhone) {
        GetCodeRequest getCodeRequest = new GetCodeRequest();
        String androidId = AndroidUtils.getAndroidId(DaggerApplication.getIns());

        getCodeRequest.setDevice_id(androidId);
        getCodeRequest.setChannel_id("000001");
        getCodeRequest.setType_id("5");
        getCodeRequest.setMobile(userPhone);
        String json = new Gson().toJson(getCodeRequest);
        L.e(Constant.TAG, "getCode:" + json);
        mNetworkDateSource.getSms(getCodeRequest, new SubscribeResult<BaseBean>() {
            @Override
            protected void onFailure(BaseBean response) {

//                ((IGetCodeContact.IGetCodeView) mView).getCodeFail(response.getMessage());
            }

            @Override
            protected void onOk(BaseBean response) {

//                PubUtils.popTipOrWarn(DaggerApplication.getIns(), response.getMessage());
//                ((IGetCodeContact.IGetCodeView) mView).updataSuccess();
            }

            @Override
            protected void onNext(BaseBean response) {
//                PubUtils.popTipOrWarn(DaggerApplication.getIns(), response.getMessage());
//                ((IGetCodeContact.IGetCodeView) mView).getCodeFail(response.getMessage());
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
                Toast.makeText(DaggerApplication.getIns(), throwable.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });

    }

}
